//
//  NSDictionary+KCSafety.m
//  KCDriver
//
//  Created by bjkcwl-fth on 2020/9/16.
//  Copyright © 2020 bjkcwl. All rights reserved.
//

#import "NSDictionary+KCSafety.h"

@implementation NSDictionary (KCSafety)

#pragma mark -取值(防止为Null)
- (id)safeObjectForKey:(NSString *)key {
    if (![key isKindOfClass:[NSString class]] || [[self objectForKey:key] isEqual:[NSNull null]]) {
        return nil;
    }
    return [self objectForKey:key];
}

@end

@implementation NSMutableDictionary (KCSafety)

- (void)safeRemoveObjectForKey:(NSString *)aKey {
    if ([aKey isKindOfClass:[NSString class]]) {
        [self removeObjectForKey:aKey];
    } else {
        NSLog(@"Key有误");
    }
}

- (void)safeSetObject:(id)anObject forKey:(NSString *)aKey {
    if ((anObject != nil) && [aKey isKindOfClass:[NSString class]]) {
        [self setObject:anObject forKey:aKey];
    } else {
        NSLog(@"Key/value有误");
    }
}

- (void)safeSetDictionary:(NSDictionary *)otherDictionary {
    if ([otherDictionary isKindOfClass:[NSDictionary class]] || [otherDictionary isKindOfClass:[NSMutableDictionary class]]) {
        [self setValuesForKeysWithDictionary:otherDictionary];
    } else {
        NSLog(@"dictionary有误");
    }
}

@end
